#!/opt/local/bin/python

import csv
from rfvectors import cavity
from scipy import nan

outfile = csv.writer(open('powerlist.csv', 'wb'), delimiter=',')
outfile.writerow([
    'Frequency (Hz)',
    'Synch phase',
    'Q_L',
    'R/Q',
    'R/Q scaled for beta',
    'Power to beam (W)',
    'Reflected power (W)',
    'Generator power (W)'])

#Ql = {'spoke': 1.5e5, 'medbeta': 7.973e5, 'highbeta': 7.878e5}
Ql = [1.5e5, 7.973e5, 7.878e5]

spokedata    = csv.reader(open('spokenew.csv', 'rU'), delimiter=',')
medbetadata  = csv.reader(open('lowbetanew.csv', 'rU'), delimiter=',')
highbetadata = csv.reader(open('highbetanew.csv', 'rU'), delimiter=',')

counter = 0
for cavdat in [spokedata, medbetadata, highbetadata]:
    for row in cavdat:
        V = float(row[0]) * float(row[3])
        ph = float(row[1])
        b = float(row[2])
        f= float(row[4])
        roverq = float(row[5])
        beta_0 = float(row[6])
        try:
            Eampfile = row[7]
        except IndexError:
            Eampfile = nan

        A = cavity(f=f, delF=nan, I_b0=50e-3, I_b=50e-3, V_cav=V, beamph=ph, RoverQ=roverq,
                Ql=Ql[counter], beta_0=beta_0, beta=b, Eampfile=Eampfile)
        outfile.writerow([A.f, A.beamph, A.Ql, A.RoverQ, A.RoverQ*A.RQscaling, A.Pbeam, A.Pref, A.Pg])

    counter += 1

